package cn.kgc.boot.tcuser.controller;

import cn.kgc.boot.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

@Controller
public class RedisController {
    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping(value = "testGs")
    @ResponseBody
    public String testGS(){
        String key = "haha";
        String value = "kkkkkkk";
        redisUtil.setValue(key, value);
        String valueaaa = (String)redisUtil.getValue(key);
        return valueaaa;
    }

    @RequestMapping(value = "loginTest")
    public String loginTest(){
        return "";
    }

    @ResponseBody
    @GetMapping(value = "getKey")
    public String getKey(String key){
        return redisUtil.getValue(key) == null ? null : redisUtil.getValue(key).toString();
    }

    @ResponseBody
    @GetMapping(value = "getTime")
    public Long getTime(String key){
        return redisUtil.getTime(key);
    }

    @ResponseBody
    @GetMapping(value = "setKey")
    public boolean setKey(String key){
        return redisUtil.setValue(key, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }

    @ResponseBody
    @GetMapping(value = "setTimeKey")
    public boolean setTimeKey(String key){
        return redisUtil.setTimeValue(key
                , LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
                , 10L
                , TimeUnit.SECONDS);
    }



}
